# Contributor: Mathias LANG <pro.mathias.lang@gmail.com>
# Maintainer: Mathias LANG <pro.mathias.lang@gmail.com>
pkgname=dub
pkgver=1.29.1
pkgrel=0
pkgdesc="Package and build management system for D"
url="https://code.dlang.org/"
arch="x86_64 aarch64"
license="MIT"
depends="libcurl"
makedepends="chrpath ldc bash curl-dev"
subpackages="$pkgname-zsh-completion:zsh:noarch $pkgname-doc:doc:noarch"
source="$pkgname-$pkgver.zip::https://github.com/dlang/dub/archive/v$pkgver.zip"

# pass on edge but fail on 3.16(edge) ..
[ "$CARCH" = "aarch64" ] && options="$options !check"

build() {
	# Default abuild.conf has '-Os' which gdmd doesn't support
	# Since we later build the man pages using dub, make it persistent
	DFLAGS=""

	# The build script needs a DMD-like interface to pass its arguments, so use ldmd2
	DMD=ldmd2 GITVER="v$pkgver" ldc2 -run "$builddir/build.d"

	# Build man pages
	"$builddir/bin/dub" --compiler=ldc2 --single scripts/man/gen_man.d

	# Remove redundant rpath
	chrpath -d "$builddir/bin/dub"
}

check() {
	bin/dub test
}

package() {
	install -s -D "$builddir/bin/dub" "$pkgdir/usr/bin/dub"
	install -Dm644 "$builddir/scripts/zsh-completion/_dub" "$pkgdir/usr/share/zsh/site-functions/_dub"
	mkdir -p "$pkgdir/usr/share/man/man1/"
	cp "$builddir"/scripts/man/*.1 "$pkgdir/usr/share/man/man1/"
}

zsh() {
	pkgdesc="Zsh completion for dub"
	depends=""
	install_if="$pkgname=$pkgver-r$pkgrel zsh"

	mkdir -p "$subpkgdir"/usr/share/zsh/site-functions
	mv "$pkgdir"/usr/share/zsh/site-functions/* \
		"$subpkgdir"/usr/share/zsh/site-functions
	# Satisfy abuild's linter
	rmdir "$pkgdir"/usr/share/zsh/site-functions
}

doc() {
	pkgdesc="Man pages for dub"
	depends=""
	install_if="$pkgname=$pkgver-r$pkgrel docs"

	gzip -9 "$pkgdir"/usr/share/man/man1/*
	mkdir -p "$subpkgdir"/usr/share/man/man1
	mv "$pkgdir"/usr/share/man/man1/* "$subpkgdir"/usr/share/man/man1/
	# Satisfy abuild's linter
	rmdir "$pkgdir"/usr/share/man/man1/
	rmdir "$pkgdir"/usr/share/man/
}

sha512sums="
5bf26aea27b5c7459d16461c5d62012f31d025460aa1bab6446afaaacefa9f363cf8f07b4174d71cdd76f599c01c9aa5a0db9f04f614cc00c67c3ba8758a7621  dub-1.29.1.zip
"
